$(function() {
	var $btn = $('#btn');
	var $mask = $('#mask')

	$btn.click(function() {
		$(this).attr('disabled', true);
		$mask.slideUp();
		var num = getMyPrize();
		switch(num) {
			case 1:
				rotateFunc(1, 60,'恭喜您获得了一等奖');
				break;
			case 2:
				rotateFunc(2, 0,'恭喜您获得了二等奖');
				break;
			case 3:
				rotateFunc(3, 120,'恭喜您获得了三等奖');
				break;
			case 4:
				rotateFunc(4, 240,'恭喜您获得了一等奖');
				break;
			case 5:
				rotateFunc(5, 180,'恭喜您获得了二等奖');
				break;
			case 6:
				rotateFunc(6, 300,'恭喜您获得了三等奖');
		}
	});

	var rotateFunc = function(awards, angle, text) { //awards:奖项，angle:奖项对应的角度
		/*$plateBtn.stopRotate();*/
		$btn.rotate({
			angle: 0,
			duration: 8000,
			animateTo: angle + 360 * 6, //angle是图片上各奖项对应的角度
			callback: function() {
				$mask.html(text);
				$mask.slideDown();
				$btn.attr('disabled', false)
			}
		});
	};

	/*	$resultBtn.click(function(){
			$result.hide();
		});*/
	var prizes = [2, 18, 80];
	//获取本次的奖品
	function getMyPrize() {
		var index = getWeightRandom() + 1;
		if(Math.random() > 0.5) {
			index += 3;
		}
		return index;
	}
	//根据权重随机获取数组的索引
	function getWeightRandom() {
		var weightSum = 0;
		var stepWeightSum = 0;
		for(var i = 0; i < prizes.length; i++) {
			weightSum += prizes[i];
		}
		var randVal = Math.random();
		for(var i = 0; i < prizes.length; i++) {
			stepWeightSum += prizes[i];
			if(randVal <= stepWeightSum / weightSum) {
				return i
			}
		}
		return 0
	}
	//------------------------------
	var map = {};
	for(var i = 0; i < 100; i++) {
		var s = getMyPrize();
		if(s > 3) {
			s -= 3;
		}
		if(map[s] == null) {
			map[s] = 1
		} else {
			map[s] = map[s] + 1;
		}
	}
	function closeMask(){
		$(document).click(function(){
			$mask.slideUp()
		})
	}
	closeMask();
})